package de.justphil.tcg.tcgserver.websockets.util;

import java.util.Locale;

import net.jcip.annotations.ThreadSafe;
import de.justphil.tcg.tcgserver.commons.i18n.I18n;
import de.justphil.tcg.tcgserver.commons.config.I18nConfig;
import de.justphil.tcg.tcgserver.commons.http.MessageResponseCode;
import de.justphil.tcg.tcgserver.websockets.messages.MessageSubject;
import de.justphil.tcg.tcgserver.websockets.messages.TCGErrorFragment;
import de.justphil.tcg.tcgserver.websockets.messages.TCGInfoFragment;
import de.justphil.tcg.tcgserver.websockets.messages.TCGJoinFragment;
import de.justphil.tcg.tcgserver.websockets.messages.TCGMessage;
import de.justphil.tcg.tcgserver.websockets.messages.TCGProgressFragment;

@ThreadSafe
public class MessageFactory {
	public static TCGMessage errorMessage(String errorKey) {
		return errorMessage(errorKey, new Locale(I18nConfig.DEFAULT_LANGUAGE, I18nConfig.DEFAULT_COUNTRY));
	}
	
	public static TCGMessage errorMessage(String errorKey, Locale locale) {
		return new TCGMessage(
				MessageSubject.ERROR,
				null,
				null,
				null,
				null,
				new TCGErrorFragment(
						MessageResponseCode.getResponseCodeByErrorKey(errorKey),
						I18n.translate(errorKey, locale)
				)
		);
	}
	
	public static TCGMessage infoMessage(String msgKey, int type) {
		return infoMessage(msgKey, type, new Locale(I18nConfig.DEFAULT_LANGUAGE, I18nConfig.DEFAULT_COUNTRY));
	}
	
	public static TCGMessage infoMessage(String msgKey, int type, Locale locale) {
		return new TCGMessage(
				MessageSubject.GAME_INFO,
				null,
				null,
				new TCGInfoFragment(type, msgKey, I18n.translate(msgKey, locale))
		);
	}
	
	public static TCGMessage joinAckMessage(int code) {
		return new TCGMessage(
				MessageSubject.GAME_JOIN,
				null,
				new TCGJoinFragment(code)
		);
	}
	
	public static TCGMessage progressCommandMessage(String cmd) {
		return new TCGMessage(
				MessageSubject.GAME_PROGRESS,
				null,
				null,
				null,
				new TCGProgressFragment(cmd)
		);
	}
	
	public static TCGMessage progressCommandMessage(String cmd, int slot) {
		return new TCGMessage(
				MessageSubject.GAME_PROGRESS,
				null,
				null,
				null,
				new TCGProgressFragment(cmd, slot)
		);
	}
	
	public static TCGMessage progressWinnerMessage(long winnerPlayerId) {
		return new TCGMessage(
				MessageSubject.GAME_PROGRESS,
				null,
				null,
				null,
				new TCGProgressFragment(null, winnerPlayerId)
		);
	}
	
	public static TCGMessage progressMessage(int s, long pId, long cId, int[] pT, int l, int at, int d, int ag) {
		return new TCGMessage(
				MessageSubject.GAME_PROGRESS,
				null,
				null,
				null,
				new TCGProgressFragment(s, pId, cId, pT, l, at, d, ag)
		);
	}
}
